#include "windows.h"
#include "UiTopBar.h"


extern HINSTANCE					g_SDHResModuleHandle;

UiTopBar::UiTopBar()
:m_bFirstEnter(TRUE)
{

}

UiTopBar::~UiTopBar()
{
}

BOOL UiTopBar::InitUiWin(HWND hWnd)
{
	BOOL bRtn = FALSE;
	LoadiInterItem();

/***************************************************************************/
#if 0
	HDC hdc = GetDC(hWnd);
	HDC memdc = CreateCompatibleDC(hdc);
//	m_hBakDC = CreateCompatibleDC(hdc);
	m_hBitmapBakDC = CreateCompatibleBitmap(hdc, GetWidth(), GetHeight());
	HGDIOBJ oldbmp = SelectObject(memdc, m_hBitmapBakDC);

	RECT prcWin = GetRect();
	for (int i=0; i<GetChildrenCount(); i++)
	{
		UiWin* pWin = GetChild(i);
		if(!pWin)
			continue;

		RECT rcWin = pWin->GetRect();
		OffsetRect(&rcWin, prcWin.left, prcWin.top);
		RECT rcInt = rcWin;
//		if(IntersectRect(&rcInt, prcUpdate, &rcWin)==0)
//			continue;
		printf("pWin(%d) address is 0x%x\r", i, pWin);
	}

//clean
	SelectObject(memdc, oldbmp);
	DeleteDC(memdc);
	ReleaseDC(hWnd, hdc);

#else
	HDC hdc = GetDC(hWnd);
	m_hBakDC = CreateCompatibleDC(hdc);
	m_hBitmapBakDC = CreateCompatibleBitmap(hdc, GetWidth(), GetHeight());
	HGDIOBJ oldbmp = SelectObject(m_hBakDC, m_hBitmapBakDC);

	RECT prcWin = GetRect();
	RECT prcUpdate = prcWin;
	OnPaint(m_hBakDC, &prcWin, &prcUpdate);

	ReleaseDC(hWnd, hdc);
#endif
/*****************************************************************************/
	return bRtn;
}

void UiTopBar::PaintWin(HDC hdc, RECT* prcWin, RECT* prcUpdate)
{
#if 0
	if(m_bFirstEnter)
	{
/*
		BLENDFUNCTION bfa={AC_SRC_OVER,0,255,AC_SRC_ALPHA};
		int nX = prcWin->left;
		int nY = prcWin->top;
		int nHeight = RECT_HEIGHT(*prcWin);
		int nStep = 1;
		int nPos = 0;
		RECT rcDst = *prcWin;
		//BitBlt(hdcDst, nX,nY,nWidth,nHeight,hdcBmp1,0,0,SRCCOPY);
		for (int nPos=0; nPos<=nHeight; nPos+=nStep)
		{
			AlphaBlend(hdc, nX,rcDst.top, rcDst.right, rcDst.top+nPos, m_TopBarBG->GetDC(), 0,0, rcDst.right, nPos, bfa);
		}
*/		m_bFirstEnter = FALSE;
		m_TopBarBG->Draw(hdc, prcWin, false, false);
//			StretchBlt(hdcDst, nX, nY, nWidth, nHeight, hdcBmp2, 0,0,nWidth, nHeight, SRCCOPY);
	}
	else
	{
		m_TopBarBG->Draw(hdc, prcWin, false, false);
	}
#else
	m_TopBarBG->Draw(hdc, prcWin, false, false);
#endif
}

int UiTopBar::OnLButtonDown(UINT fwKeys, int xPos, int yPos)
{
	return UiWin::OnLButtonDown(fwKeys, xPos, yPos);
}

int UiTopBar::OnLButtonUp(UINT fwKeys, int xPos, int yPos)
{
	return UiWin::OnLButtonUp(fwKeys, xPos, yPos);
}

void UiTopBar::LoadiInterItem()
{
	m_TopBarBG = ImagingHelper::GetImageObject(g_SDHResModuleHandle, 9520);

	RECT rtTopBarBtn[MAX_TOPBAR_BTN] = {
		36,5,94,43,
		715,5,757,43,
	};

	m_BtnTopBar[VIDEO_TOPBAR_CLOSE].SetPos(rtTopBarBtn[VIDEO_TOPBAR_CLOSE].left, rtTopBarBtn[VIDEO_TOPBAR_CLOSE].top, rtTopBarBtn[VIDEO_TOPBAR_CLOSE].right-rtTopBarBtn[VIDEO_TOPBAR_CLOSE].left, rtTopBarBtn[VIDEO_TOPBAR_CLOSE].bottom-rtTopBarBtn[VIDEO_TOPBAR_CLOSE].top);
	m_BtnTopBar[VIDEO_TOPBAR_CLOSE].SetID(8500);
	m_BtnTopBar[VIDEO_TOPBAR_CLOSE].SetNormalImage(ImagingHelper::GetImageObject(g_SDHResModuleHandle,9502, true));
	m_BtnTopBar[VIDEO_TOPBAR_CLOSE].SetPressedImage(ImagingHelper::GetImageObject(g_SDHResModuleHandle,9503, true));
	AddChild(&m_BtnTopBar[VIDEO_TOPBAR_CLOSE]);

	m_BtnTopBar[VIDEO_TOPBAR_FILEBROWSER].SetPos(rtTopBarBtn[VIDEO_TOPBAR_FILEBROWSER].left, rtTopBarBtn[VIDEO_TOPBAR_FILEBROWSER].top, rtTopBarBtn[VIDEO_TOPBAR_FILEBROWSER].right-rtTopBarBtn[VIDEO_TOPBAR_FILEBROWSER].left, rtTopBarBtn[VIDEO_TOPBAR_FILEBROWSER].bottom-rtTopBarBtn[VIDEO_TOPBAR_FILEBROWSER].top);
	m_BtnTopBar[VIDEO_TOPBAR_FILEBROWSER].SetID(8501);
	m_BtnTopBar[VIDEO_TOPBAR_FILEBROWSER].SetNormalImage(ImagingHelper::GetImageObject(g_SDHResModuleHandle,9506, true));
	m_BtnTopBar[VIDEO_TOPBAR_FILEBROWSER].SetPressedImage(ImagingHelper::GetImageObject(g_SDHResModuleHandle,9507, true));
	AddChild(&m_BtnTopBar[VIDEO_TOPBAR_FILEBROWSER]);


	RECT rtProgress = {225, 16, 575, 40};
	m_slideBar[VIDEO_TOPBAR_STATUSBAR_PROGRESS].SetPos(rtProgress.left, rtProgress.top,
		rtProgress.right-rtProgress.left, 
		rtProgress.bottom-rtProgress.top);
	m_slideBar[VIDEO_TOPBAR_STATUSBAR_PROGRESS].SetStyle(SliderBar);
	m_slideBar[VIDEO_TOPBAR_STATUSBAR_PROGRESS].SetID(8502);
	m_slideBar[VIDEO_TOPBAR_STATUSBAR_PROGRESS].SetSliderBitmap( ImagingHelper::GetImageObject(g_SDHResModuleHandle,9504, true),
															ImagingHelper::GetImageObject(g_SDHResModuleHandle,9505, true),
															ImagingHelper::GetImageObject(g_SDHResModuleHandle,9519, true),
															24, 24);
	m_slideBar[VIDEO_TOPBAR_STATUSBAR_PROGRESS].SetPadpos( 50 );
	AddChild(&m_slideBar[VIDEO_TOPBAR_STATUSBAR_PROGRESS]);


	RECT rtSongInfo[MAX_TOPBAR_STATIC_CTRL] = {
		100, 15, 210, 40,
		590, 15, 700, 40,
	};
	m_staticCtl[VIDEO_TOPBAR_CURRENTTIME].SetPos(rtSongInfo[VIDEO_TOPBAR_CURRENTTIME].left, rtSongInfo[VIDEO_TOPBAR_CURRENTTIME].top,
				rtSongInfo[VIDEO_TOPBAR_CURRENTTIME].right-rtSongInfo[VIDEO_TOPBAR_CURRENTTIME].left,
				rtSongInfo[VIDEO_TOPBAR_CURRENTTIME].bottom-rtSongInfo[VIDEO_TOPBAR_CURRENTTIME].top);
	m_staticCtl[VIDEO_TOPBAR_CURRENTTIME].SetID(8503);
	m_staticCtl[VIDEO_TOPBAR_CURRENTTIME].SetTextColor(RGB(255, 255, 255));
	m_staticCtl[VIDEO_TOPBAR_CURRENTTIME].SetText(L"00:00");
	AddChild(&m_staticCtl[VIDEO_TOPBAR_CURRENTTIME]);

	m_staticCtl[VIDEO_TOPBAR_DURATIONTIME].SetPos(rtSongInfo[VIDEO_TOPBAR_DURATIONTIME].left, rtSongInfo[VIDEO_TOPBAR_DURATIONTIME].top,
				rtSongInfo[VIDEO_TOPBAR_DURATIONTIME].right-rtSongInfo[VIDEO_TOPBAR_DURATIONTIME].left,
				rtSongInfo[VIDEO_TOPBAR_DURATIONTIME].bottom-rtSongInfo[VIDEO_TOPBAR_DURATIONTIME].top);
	m_staticCtl[VIDEO_TOPBAR_DURATIONTIME].SetID(8504);
	m_staticCtl[VIDEO_TOPBAR_DURATIONTIME].SetTextColor(RGB(255, 255, 255));
	m_staticCtl[VIDEO_TOPBAR_DURATIONTIME].SetText(L"00:00");
	AddChild(&m_staticCtl[VIDEO_TOPBAR_DURATIONTIME]);


}

BOOL UiTopBar::InitImages()
{
	return TRUE;
}
